package com.sunfeng.chap2;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * 验证long write(ByteBuffer[] srcs, int offset, int length)方法将ByteBuffer的remaining写入通道
 */
public class Test5_2 {
    public static void main(String[] args) throws IOException {
        FileOutputStream fileOutputStream = new FileOutputStream(new File("d:\\52.txt"));

        FileChannel fileChannel = fileOutputStream.getChannel();

        ByteBuffer buffer1 = ByteBuffer.wrap("abcde".getBytes());

        ByteBuffer buffer2 = ByteBuffer.wrap("12345".getBytes());
        buffer2.position(1);
        buffer2.limit(3);

        ByteBuffer buffer3 = ByteBuffer.wrap("qwwqq".getBytes());

        buffer3.position(1);
        buffer3.limit(4);
        ByteBuffer[] byteBuffer = new ByteBuffer[]{buffer1, buffer2, buffer3};
        fileChannel.write(byteBuffer,0,3);

        fileChannel.close();
        fileOutputStream.close();
    }
}
